package com.yowasa.xiuxian.service.helper;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.po.*;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.mapper.XiuxianExploreResourceMapper;
import com.yowasa.xiuxian.mapper.XiuxianExploreUserMapper;
import com.yowasa.xiuxian.mapper.XiuxianExtendMapper;
import com.yowasa.xiuxian.mapper.XiuxianMapInfoMapper;
import com.yowasa.xiuxian.util.DateXUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
public class ExploreHelper {

    @Autowired
    private XiuxianExploreUserMapper xiuxianExploreUserMapper;

    @Autowired
    private XiuxianMapInfoMapper xiuxianMapInfoMapper;

    @Autowired
    private XiuxianExploreResourceMapper xiuxianExploreResourceMapper;

    public XiuXianExploreUser selectUser(Long id) {
        XiuXianExploreUser exploreUser = xiuxianExploreUserMapper.selectOne(new LambdaQueryWrapper<XiuXianExploreUser>().eq(XiuXianExploreUser::getAccountId, id).last(" for update"));
        if(ObjectUtil.isNull(exploreUser)){
            exploreUser = new XiuXianExploreUser();
            exploreUser.setAccountId(id);
            exploreUser.setGoldNum(0);
            exploreUser.setLockTime(new Date());
            exploreUser.setStatus(0);
            exploreUser.setKarma(0);
            xiuxianExploreUserMapper.insert(exploreUser);
        }
        return exploreUser;
    }

    public List<XiuxianMapInfoPO> selectMapInfoByParent(Integer local) {
        return xiuxianMapInfoMapper.selectList(new LambdaQueryWrapper<XiuxianMapInfoPO>().eq(XiuxianMapInfoPO::getParent,local).eq(XiuxianMapInfoPO::getMapType,5));
    }

    public List<XiuXianExploreResource> selectResourceByMapId(Integer mapId) {
        List<XiuXianExploreResource> resources = xiuxianExploreResourceMapper.selectList(new LambdaQueryWrapper<XiuXianExploreResource>()
                .eq(XiuXianExploreResource::getMapId, mapId)
                .gt(XiuXianExploreResource::getNowNum, 0));
        return resources;
    }

    public List<XiuXianExploreUser> selectLocalUserByMapId(Integer mapId, Long id) {
        return xiuxianExploreUserMapper.selectLocalUserByMapId(mapId,id);
    }


    public void checkCd(XiuXianExploreUser exploreUser) {
        if (exploreUser.getLockTime().compareTo(new Date()) > 0) {
            throw new HandlerException(exploreUser.getLockReason());
        }
        if(exploreUser.getStatus() != 1){
            throw new HandlerException("你还没有进入探索状态呢");
        }
    }

    public XiuxianMapInfoPO checkMap(Integer mapId, XiuXianExploreUser exploreUser) {
        checkCd(exploreUser);
        XiuxianMapInfoPO xiuxianMapInfoPO = xiuxianMapInfoMapper.selectById(mapId);
        if (xiuxianMapInfoPO.getParent() != exploreUser.getExploreMap()){
            throw new HandlerException("当前位置无法探索 ["+xiuxianMapInfoPO.getName()+"]");
        }
        if(exploreUser.getMapId() ==null || exploreUser.getMapId() != mapId){
            exploreUser.setMapId(xiuxianMapInfoPO.getId().intValue());
            exploreUser.setMapName(xiuxianMapInfoPO.getName());
            Date to = DateXUtil.getTo(3);
            exploreUser.setLockTime(to);
            exploreUser.setLockReason("正在前往探索地点 ["+xiuxianMapInfoPO.getName()+"] 的路上。 ["+DateXUtil.formatTime(to)+"]");
            xiuxianExploreUserMapper.updateById(exploreUser);
        }
        return xiuxianMapInfoPO;
    }

    public XiuXianExploreResource checkResource(Integer mapId, Integer resourceId, XiuXianExploreUser exploreUser) {
        checkCd(exploreUser);
        XiuxianMapInfoPO xiuxianMapInfoPO = xiuxianMapInfoMapper.selectById(mapId);
        XiuXianExploreResource xiuXianExploreResource = xiuxianExploreResourceMapper.selectOne(new LambdaQueryWrapper<XiuXianExploreResource>().eq(XiuXianExploreResource::getId,resourceId).last(" for update"));
        if(exploreUser.getMapId() ==null || exploreUser.getMapId() != mapId){
            throw new HandlerException("你当前不在 ["+xiuxianMapInfoPO.getName()+"] ,无法进行探索");
        }
        if(xiuXianExploreResource.getNowNum() <= 0){
            throw new HandlerException("来迟了，["+xiuXianExploreResource.getResourceName()+"]已经没有啦");
        }
        return xiuXianExploreResource;
    }

    public void checkBattle(XiuXianExploreUser exploreUser, XiuXianExploreUser exploreBattleUser) {
        checkCd(exploreUser);
        checkCd(exploreBattleUser);
        if(exploreUser.getMapId() != exploreBattleUser.getMapId()){
            throw new HandlerException("你们不在同一个位置哦，无法打劫");
        }
    }


    public void lockUser(XiuXianExploreUser userBO, Integer days, String reason) {
        Date to = DateXUtil.getTo(days);
        userBO.setLockTime(to);
        userBO.setLockReason(reason + " [" + DateXUtil.formatTime(to) + "]");
    }

    public void updateUser(XiuXianExploreUser exploreUser) {
        xiuxianExploreUserMapper.updateById(exploreUser);
    }

    public void updateResource(XiuXianExploreResource xiuXianExploreResource) {
        xiuxianExploreResourceMapper.updateById(xiuXianExploreResource);
    }

    public void insertSacrifice(SacrificeLog sacrificeLog) {
        xiuxianExploreUserMapper.insertSacrifice(sacrificeLog);
    }

    public Integer countSacrificeByStage(Long id, Integer stage) {
       return xiuxianExploreUserMapper.countSacrificeByStage(id,stage);
    }

    public Integer countSacrificeByWeek(Long id) {
        return xiuxianExploreUserMapper.countSacrificeByWeek(id);
    }

    public void insertBattleLog(ExploreBattleLog battleLog) {
        xiuxianExploreUserMapper.insertBattleLog(battleLog);
    }

    public List<ExploreBattleLog> selectSize(Long id, int i) {
        Integer offset = i * 10;
        return xiuxianExploreUserMapper.selectBattleLogList(id,offset);
    }
}
